<?php
function  createJGWayBill($pars){
	//制单人
	$optid=1;
	$opDate=gmtime();
	//承运人
	$carrier_id=12;
	$carrier="全球捷运";

	$status=0;
	$bill_id=-1;
	$vhcCode=$pars->vhc?$pars->vhc:"";  //車牌号
	$B_JH_DD=$pars->B_JH_DD?$pars->B_JH_DD:'';
	$E_JH_DD=$pars->E_JH_DD?$pars->E_JH_DD:'';
	$B_ADD=$pars->B_ADD?$pars->B_ADD:'';
	$E_ADD=$pars->E_ADD?$pars->E_ADD:'';
	$SHIP =$pars->SHIP_NO?$pars->SHIP_NO:'';
	$OVOY =$pars->OVOY?$pars->OVOY:'';
	$TD_NO=$pars->TD_NO?$pars->TD_NO:'';
	$SR_NO=$pars->SR_NO?$pars->SR_NO:'';
	$BOX =$pars->BOX?$pars->BOX:"";
	$wm_trailer=$pars->gvhc?$pars->gvhc:""; //挂车号
	$LINK=$pars->LINK?$pars->LINK:'';
	$TEL=$pars->TEL?$pars->TEL:'';
	$B_LINK=$pars->B_LINK?$pars->B_LINK:'';
	$B_TEL=$pars->B_TEL?$pars->B_TEL:'';
	$E_LINK=$pars->E_LINK?$pars->E_LINK:'';
	$E_TEL=$pars->E_TEL?$pars->E_TEL:'';
	try  {
		if(empty($BOX)){
			throw new Exception("请输入集装箱信息");
		}
		$a_member=&m("member");
		//$member=$a_member->get($UID);
		$member=$a_member->get("vhcCode='".$vhcCode."'");
		if(empty($member)===false){
			$UID=$member['user_id'];
			$channelId=$member['channelId'];
			$vhcCode=$member['vhcCode'];
			$driver=$member['real_name'];
			$drivertel=$member['phone_tel'];
		}else
		$UID=0;

		$waybill=&m('waybill');
		$goodstrunk=&m('goodstrunk');
		$goods_trunk_sub=&m('goodstrunksub');
		$waybilltrunk=&m('waybilltrunk');

		//回写集装箱号
		$wm_ctnno = "";
		foreach($BOX as $K=>$v){
			$SR_NO =$v->SR_NO?$v->SR_NO:'';
			if(!empty($SR_NO)){
				$wm_ctnno.=$SR_NO.'|';
			}
		}

		//写入运单主表
		$data_main=array('wm_stauts'=>'已派',
				'wm_type'=>'集港',
				'wm_code'=>autocode(),
				'wm_trailer'=>$wm_trailer,
				'wm_loading_place'=>$B_ADD,
	        	'wm_loading_place'=>$B_ADD,
				'wm_loading_date'=>gmstr2time($B_JH_DD),//date('Y-m-d H:i',),
				'wm_unloading_place'=>$E_ADD,
				'wm_optid'=>$optid,
				'wm_opdate'=>$opDate,
				'wm_ship'=>$SHIP,
				'wm_voyage'=>$OVOY,
				'wm_awb_code'=>$TD_NO,
				'wm_user'=>$UID,
				'wm_carrier_id'=>$carrier_id,
				'wm_carrier'=>$carrier,
				'wm_vhc_code'=>$vhcCode,
				'wm_driver'=>$driver,
				'wm_driver_tel'=>$drivertel,
				'wm_dispatch_date'=>gmtime(),
				'wm_dispatch_person'=>$LINK,
				'wm_dispatch_person_tel'=>$TEL,
				'wm_ctnno'=>$wm_ctnno
		);

		$bill_id = $waybill->add($data_main);

		//写入货物
		$data_goods=array(
			'g_type'=>'集港',
			'g_outkey'=>$SR_NO,
			'g_awb_code'=>$TD_NO,
			'g_ship'=>$SHIP,	
			'g_status'=>'已派',
			'g_voyage'=>$OVOY,
			'g_optid'=>$optid,
			'g_opdate'=>$opDate,
			'g_collectiontime'=>gmstr2time($E_JH_DD) ,//date('Y-m-d H:i',),
			'g_packingtime'=>gmstr2time($B_JH_DD),//date('Y-m-d H:i',),
			'g_container_depot'=>$B_ADD,
			'g_container_man'=>$B_LINK,
			'g_container_tel'=>$B_TEL,
			'g_full_container_depot'=>$E_ADD,
			'g_full_container_man'=>$E_LINK,
			'g_full_container_tel'=>$E_TEL,
			'g_loading_place'=>$B_ADD,
			'g_loading_man'=>$B_LINK,
			'g_loading_tel'=>$B_TEL,
		);
		$gid=$goodstrunk->add($data_goods);
			
		foreach($BOX as $K=>$v){
			$SR_NO =$v->SR_NO?$v->SR_NO:'';
			$CX =$v->CX?$v->CX:'';
			$QTY =$v->QTY?$v->QTY:0;
			$ITM =$v->ITM?$v->ITM:0;
			$BOX_NO =$v->BOX_NO?$v->BOX_NO:'';

			//写入箱型箱量
			$data_goods_sub=array('g_ctn_no'=>$BOX_NO,'gs_gtid'=>$gid,'g_box'=>$CX,'g_volume'=>$QTY,'g_outkey'=>$SR_NO,'g_itm'=>$ITM,);
			$gsid=$goods_trunk_sub->add($data_goods_sub);
			$waybilltrunk->add(array('ws_wmtid'=>$bill_id,'ws_gid'=>$gid,'ws_gstid'=>$gsid,'ws_volume'=>$QTY));
		}
		//TODO: 添加运程
		addhauldeploy(4,$bill_id,'集港',true,null,null,null,null);
		//TODO:MESSAGE
		$channelId=null;
		$bill=$waybill->find("ifnull(wm_accepted,0)=0  and ifnull(wm_stauts,'新建')='已派' and ifnull(wm_msg_arrive,'0')=0 and wm_tid=".$bill_id);
		if(empty($bill)===false){
			foreach($bill as $k=>$w){
				$wid=$w['wm_tid'];
				if($w['wm_type']=='疏港') {
					$sql="select * from zys_goods_trunk where exists(select 1 from zys_waybill_sub_trunk where g_tid=ws_gid and ws_wmtid=".$wid.")";
					$gt=$waybill->getRow($sql);
					pushMsg($channelId,
							'{"type":"3","result":{"wm_tid":"' . $wid . '","type":"' . $w['wm_type'] . '","note":"' . $w['wm_requiredl_note'] . ' ' . local_date('Y年m月d日 H点i分', $gt['g_collectiontime']) . '到' . $gt['g_full_container_depot'] . '背重箱
					' . local_date('Y年m月d日 H点i分', $gt['g_packingtime']) . '送至' . $gt['g_loading_place'] . '","voice":"' . $w['wm_requiredl_note_voice'] . '","intral":"30"}}', $opts);
				}
				else if($w['wm_type']=='集港') {
					$sql="select * from zys_goods_trunk where exists(select 1 from zys_waybill_sub_trunk where g_tid=ws_gid and ws_wmtid=".$wid.")";
					$gt=$waybill->getRow($sql);
					pushMsg($channelId,
							'{"type":"3","result":{"wm_tid":"' . $wid . '","type":"' . $w['wm_type'] . '","note":"' . $w['wm_requiredl_note'] . ' ' . local_date('Y年m月d日 H点i分', $gt['g_packingtime']) . '到' . $gt['g_loading_place'] . '装货
					' . local_date('Y年m月d日 H点i分', $gt['g_collectiontime']) . '前送至' . $gt['g_full_container_depot'] . '","voice":"' . $w['wm_requiredl_note_voice'] . '","intral":"30"}}', $opts);
				}
				else {
					$sql="select * from zys_goods_normal where exists(select 1 from zys_waybill_sub_normal where g_tid=ws_gid and ws_wmtid=".$wid.")";
					$gt=$waybill->getRow($sql);
					pushMsg($channelId,
							'{"type":"3","result":{"wm_tid":"' . $wid . '","type":"' . $gt['g_type'] . '","note":"' . $w['wm_requiredl_note'] . ' ' . local_date('Y年m月d日 H点i分', $gt['g_loading_date']) . '到' . $gt['g_loading_place'] . '提货
					' . local_date('Y年m月d日 H点i分', $gt['g_unloading_date']) . '送至' . $gt['g_unloading_place'] . '","voice":"' . $w['wm_requiredl_note_voice'] . '","intral":"30"}}', $opts);
				}
			}
		}
		$status=1;
		$rnt=array("wm_tid"=>$bill_id);
	}
	catch (Exception $exp){
		$rnt=$exp->getMessage();
		return $rnt;
	}
	return JSONResultStr($status,$rnt,$pars);
}

//创建疏港 mxb 0530
function createSGWayBill($pars){
	//制单人
	$optid=1;
	$opDate=gmtime();
	//承运人
	$carrier_id=12;
	$carrier="全球捷运";

	$status=0;
	$bill_id=-1;
	$vhcCode=$pars->vhc?$pars->vhc:"";  //車牌号
	$B_JH_DD=$pars->B_JH_DD?$pars->B_JH_DD:'';
	$E_JH_DD=$pars->E_JH_DD?$pars->E_JH_DD:'';
	$B_ADD=$pars->B_ADD?$pars->B_ADD:'';
	$E_ADD=$pars->E_ADD?$pars->E_ADD:'';
	$SHIP =$pars->SHIP_NO?$pars->SHIP_NO:'';
	$OVOY =$pars->OVOY?$pars->OVOY:'';
	$TD_NO=$pars->TD_NO?$pars->TD_NO:'';
	$SR_NO=$pars->SR_NO?$pars->SR_NO:'';
	$BOX =$pars->BOX?$pars->BOX:"";
	$wm_trailer=$pars->gvhc?$pars->gvhc:""; //挂车号
	$LINK=$pars->LINK?$pars->LINK:'';
	$TEL=$pars->TEL?$pars->TEL:'';
	$B_LINK=$pars->B_LINK?$pars->B_LINK:'';
	$B_TEL=$pars->B_TEL?$pars->B_TEL:'';
	$E_LINK=$pars->E_LINK?$pars->E_LINK:'';
	$E_TEL=$pars->E_TEL?$pars->E_TEL:'';
	try  {
		if(empty($BOX)){
			throw new Exception("请输入集装箱信息");
		}
		$a_member=&m("member");
		//$member=$a_member->get($UID);
		$member=$a_member->get("vhcCode='".$vhcCode."'");
		if(empty($member)===false){
			$UID=$member['user_id'];
			$channelId=$member['channelId'];
			$vhcCode=$member['vhcCode'];
			$driver=$member['real_name'];
			$drivertel=$member['phone_tel'];
		}else
		$UID=0;

		$waybill=&m('waybill');
		$goodstrunk=&m('goodstrunk');
		$goods_trunk_sub=&m('goodstrunksub');
		$waybilltrunk=&m('waybilltrunk');

		//回写集装箱号
		$wm_ctnno = "";
		foreach($BOX as $K=>$v){
			$SR_NO =$v->SR_NO?$v->SR_NO:'';
			if(!empty($SR_NO)){
				$wm_ctnno.=$SR_NO.'|';
			}
		}

		//写入运单主表
		$data_main=array('wm_stauts'=>'已派',
				'wm_type'=>'疏港',
				'wm_code'=>autocode(),
				'wm_trailer'=>$wm_trailer,
				'wm_loading_place'=>$B_ADD,
	        	'wm_loading_place'=>$B_ADD,
				'wm_loading_date'=>gmstr2time($B_JH_DD),//date('Y-m-d H:i',),
				'wm_unloading_place'=>$E_ADD,
				'wm_optid'=>$optid,
				'wm_opdate'=>$opDate,
				'wm_ship'=>$SHIP,
				'wm_voyage'=>$OVOY,
				'wm_awb_code'=>$TD_NO,
				'wm_user'=>$UID,
				'wm_carrier_id'=>$carrier_id,
				'wm_carrier'=>$carrier,
				'wm_vhc_code'=>$vhcCode,
				'wm_driver'=>$driver,
				'wm_driver_tel'=>$drivertel,
				'wm_dispatch_date'=>gmtime(),
				'wm_dispatch_person'=>$LINK,
				'wm_dispatch_person_tel'=>$TEL,
				'wm_ctnno'=>$wm_ctnno
		);

		$bill_id = $waybill->add($data_main);

		//写入货物
		$data_goods=array(
			'g_type'=>'疏港',
			'g_outkey'=>$SR_NO,
			'g_awb_code'=>$TD_NO,
			'g_ship'=>$SHIP,	
			'g_status'=>'已派',
			'g_voyage'=>$OVOY,
			'g_optid'=>$optid,
			'g_opdate'=>$opDate,
			'g_collectiontime'=>gmstr2time($E_JH_DD) ,//date('Y-m-d H:i',),
			'g_packingtime'=>gmstr2time($B_JH_DD),//date('Y-m-d H:i',),
			'g_container_depot'=>$B_ADD,
			'g_container_man'=>$B_LINK,
			'g_container_tel'=>$B_TEL,
			'g_full_container_depot'=>$E_ADD,
			'g_full_container_man'=>$E_LINK,
			'g_full_container_tel'=>$E_TEL,
			'g_loading_place'=>$B_ADD,
			'g_loading_man'=>$B_LINK,
			'g_loading_tel'=>$B_TEL,
		);
		$gid=$goodstrunk->add($data_goods);
			
		foreach($BOX as $K=>$v){
			$SR_NO =$v->SR_NO?$v->SR_NO:'';
			$CX =$v->CX?$v->CX:'';
			$QTY =$v->QTY?$v->QTY:0;
			$ITM =$v->ITM?$v->ITM:0;
			$BOX_NO =$v->BOX_NO?$v->BOX_NO:'';

			//写入箱型箱量
			$data_goods_sub=array('g_ctn_no'=>$BOX_NO,'gs_gtid'=>$gid,'g_box'=>$CX,'g_volume'=>$QTY,'g_outkey'=>$SR_NO,'g_itm'=>$ITM,);
			$gsid=$goods_trunk_sub->add($data_goods_sub);
			$waybilltrunk->add(array('ws_wmtid'=>$bill_id,'ws_gid'=>$gid,'ws_gstid'=>$gsid,'ws_volume'=>$QTY));
		}
		//TODO: 添加运程
		addhauldeploy(8,$bill_id,'疏港',true,null,null,null,null);
		//TODO:MESSAGE
		$channelId=null;
		$bill=$waybill->find("ifnull(wm_accepted,0)=0  and ifnull(wm_stauts,'新建')='已派' and ifnull(wm_msg_arrive,'0')=0 and wm_tid=".$bill_id);
		if(empty($bill)===false){
			foreach($bill as $k=>$w){
				$wid=$w['wm_tid'];
				if($w['wm_type']=='疏港') {
					$sql="select * from zys_goods_trunk where exists(select 1 from zys_waybill_sub_trunk where g_tid=ws_gid and ws_wmtid=".$wid.")";
					$gt=$waybill->getRow($sql);
					pushMsg($channelId,
							'{"type":"3","result":{"wm_tid":"' . $wid . '","type":"' . $w['wm_type'] . '","note":"' . $w['wm_requiredl_note'] . ' ' . local_date('Y年m月d日 H点i分', $gt['g_collectiontime']) . '到' . $gt['g_full_container_depot'] . '背重箱
					' . local_date('Y年m月d日 H点i分', $gt['g_packingtime']) . '送至' . $gt['g_loading_place'] . '","voice":"' . $w['wm_requiredl_note_voice'] . '","intral":"30"}}', $opts);
				}
				else if($w['wm_type']=='集港') {
					$sql="select * from zys_goods_trunk where exists(select 1 from zys_waybill_sub_trunk where g_tid=ws_gid and ws_wmtid=".$wid.")";
					$gt=$waybill->getRow($sql);
					pushMsg($channelId,
							'{"type":"3","result":{"wm_tid":"' . $wid . '","type":"' . $w['wm_type'] . '","note":"' . $w['wm_requiredl_note'] . ' ' . local_date('Y年m月d日 H点i分', $gt['g_packingtime']) . '到' . $gt['g_loading_place'] . '装货
					' . local_date('Y年m月d日 H点i分', $gt['g_collectiontime']) . '前送至' . $gt['g_full_container_depot'] . '","voice":"' . $w['wm_requiredl_note_voice'] . '","intral":"30"}}', $opts);
				}
				else {
					$sql="select * from zys_goods_normal where exists(select 1 from zys_waybill_sub_normal where g_tid=ws_gid and ws_wmtid=".$wid.")";
					$gt=$waybill->getRow($sql);
					pushMsg($channelId,
							'{"type":"3","result":{"wm_tid":"' . $wid . '","type":"' . $gt['g_type'] . '","note":"' . $w['wm_requiredl_note'] . ' ' . local_date('Y年m月d日 H点i分', $gt['g_loading_date']) . '到' . $gt['g_loading_place'] . '提货
					' . local_date('Y年m月d日 H点i分', $gt['g_unloading_date']) . '送至' . $gt['g_unloading_place'] . '","voice":"' . $w['wm_requiredl_note_voice'] . '","intral":"30"}}', $opts);
				}
			}
		}
		$status=1;
		$rnt=array("wm_tid"=>$bill_id);
	}
	catch (Exception $exp){
		$rnt=$exp->getMessage();
		return $rnt;
	}
	return JSONResultStr($status,$rnt,$pars);
}

function autocode(){
	$db=&db();
	$code='';
	$sql="select * from zys_dictionary where d_code='wm_code'";
	$gz=$db->getRow($sql);
	if($gz){
		$code.=$gz['d_assist'];
		$code.=($gz['d_value']+1);
	}
	$sql="select 1 from zys_waybill_main where wm_code='".$code."'";
	$way=$db->getOne($sql);
	if($way){
		$this->autocode();
	}
	$sql="UPDATE zys_dictionary SET d_value=d_value+1 WHERE d_code='wm_code'";
	$db->query($sql);

	return $code;
}
/**
 * $B_JH_DD --计划开始时间
 ,E_JH_DD  --计划结束时间
 , B_ADD   --起运地
 ,E_ADD   --目的地
 ,'疏港' YWLB   --业务类别
 ,CX  --箱型尺寸
 ,QTY   --箱量
 ,SHIP_OVOY   --船次
 ,TD_NO    --提单号
 ,SR_NO   --计划号
 ,ITM   --计划序号
 ,PG_ZT   --运输状态
 */
/**
 * 推送消息
 * @param unknown_type $pars
 */


function pushMessage($pars){
	$vhcCode=$pars->vhc?$pars->vhc:"";  //車牌号
	$B_JH_DD=$pars->B_JH_DD?$pars->B_JH_DD:'';
	$E_JH_DD=$pars->E_JH_DD?$pars->E_JH_DD:'';
	$B_ADD=$pars->B_ADD?$pars->B_ADD:'';
	$E_ADD=$pars->E_ADD?$pars->E_ADD:'';
	$SHIP =$pars->SHIP_OVOY?$pars->SHIP_OVOY:'';
	$SR_NO =$pars->SR_NO?$pars->SR_NO:'';
	$ITM =$pars->ITM?$pars->ITM:0;
	$TD_NO =$pars->TD_NO?$pars->TD_NO:'';
	$B_LINK=$pars->B_LINK?$pars->B_LINK:'';
	$B_TEL=$pars->B_TEL?$pars->B_TEL:'';
	$E_LINK=$pars->E_LINK?$pars->E_LINK:'';
	$E_TEL=$pars->E_TEL?$pars->E_TEL:'';

	$msg='{"type":"4","result":{"SR_NO":"'.$SR_NO.'","ITM":"'.$ITM.'"}}';

	if(empty($vhcCode)===false){
		$a_member=&m("member");
		$member=$a_member->get("vhcCode='".$vhcCode."'");
		if(empty($member)===false){
			$channelId=$member['channelId'];
			if(empty($channelId)===false){
				return	pushMsg($channelId,$msg,0);
			}
		}
	}
	return pushMsg(null,$msg,0);
}

/**
 * 验箱操作
 * @param unknown_type $pars
 */
function checkbox($pars){
	$vhcCode=$pars->vhc?$pars->vhc:"";  //車牌号
	$wmtid=$pars->wmtid?$pars->wmtid:0;
	$address=$pars->address?$pars->address:"";  //目的地
	$linkman=$pars->linkman?$pars->linkman:"";
	$linkman_tel=$pars->linktel?$pars->linktel:"";
	$dt=$pars->dt?$pars->dt:"";
	if(!empty($dt)){
		$dt=local_strtotime($dt);
	}
	$lat=$pars->lat?$pars->lat:0;
	$lng=$pars->lng?$pars->lng:0;
	$uid=$pars->uid?$pars->uid:0;
	$a_member=&m("member");
	
	if(empty($wmtid)){
		
		$member=$a_member->get("vhcCode='".$vhcCode."'");
		if(empty($member)){
			return JSONResultStr(0,"当前车辆没有在执行运单",$pars);
		}else{
			$wmtid=$member['current_waybill'];
			$channelId=$member['channelId'];
		}
	}
	
	// mxb 0530
	addhauldeploy(26,$wmtid,$type,false,$address,$linkman,$linkman_tel,$dt); //追加新运程
 

	$waybill=&m('waybill');
	$bill=$waybill->get("wm_tid=$wmtid");
	if(empty($bill)===false){
		$tid=$bill['wm_haul_id'];

		//完成运单 mxb 0530
		//$data=array('wm_complete'=>1,'wm_complete_time'=>gmtime(),'wm_stauts'=>'已完成');
		//$waybill->edit($wmtid,$data);
		
		//$a_member->edit($pars->uid,array('current_waybill'=>0));
		//完成运单 mxb 0530
		
		return haulSubmit($tid,$lat,$lng,$uid,$channelId);
	}
	return JSONResultStr(0,"验箱操作失败",$pars);
}


/**
 * 运程确认
 * @param unknown_type $pars
 * @throws Exception
 * @return string
 */
function haulSubmit($tid,$lat,$lng,$uid,$channelId){
	$status=0;
	try{
		$rnt=updateHaul($tid, 2, $lat, $lng, null, $uid,null);
		if($rnt===true){
			$status=1;
			$rnt="验证成功";
			if(!empty($channelId)){
				$msg='{"type":"99","result":""}';
				pushMsg($channelId, $msg, 0);
			}
		} else $rnt="验证失败";
	}
	catch (Exception $exp){
		$rnt=$exp->getMessage();
	}
	return JSONResultStr($status,$rnt,$pars);
}

/**
 * 运程确认
 * @param unknown_type $pars
 * @throws Exception
 * @return string
 */
function  updateHaul($tid,$status,$lat,$lng,$attr,$uid,$files){
	//	$haulinfo=array(
	//		'h_finished'=>$status,
	//		'h_finished_date'=>gmtime(),
	//		'h_finished_lng'=>$lng,
	//		'h_finished_lat'=>$lat,
	//		'h_finished_pid'=>$uid);
	$haul=&m('haul');
	//$haul->edit('h_tid='.$tid,$haulinfo);
	$haulinfo=$haul->get($tid);
	$haul_info=$haul->getRow('select * from zys_haul m where  h_wmtid='.$haulinfo['h_wmtid'].' and ( ifnull(h_finished,0)=0 or ifnull(h_finished,0)=1) order by h_order desc limit 0,1');
	if(empty($haul_info)===false){
		$haul->edit($haul_info['h_tid'],array('h_finished'=>1));
		$waybill=&m('waybill');
		$waybill->edit($haul_info['h_wmtid'],array('wm_haul_id'=>$haul_info['h_tid']));
		$hauls=$haul->find('h_wmtid='.$haulinfo['h_wmtid'].' and ifnull(h_finished,0)<>2 and  h_tid<>'.$haul_info['h_tid'] );
		if(!empty($hauls)){
			$h_finished_date=gmtime();
			foreach ($hauls as $k=>$v){
				$v['h_finished']=$status;
				$v['h_finished_date']=$h_finished_date;
				$v['h_finished_lng']=$lng;
				$v['h_finished_lat']=$lat;
				$v['h_finished_pid']=$uid;
				$haul->edit('h_tid='.$v['h_tid'],$v);
			}}
	}
	$rnt=$haul->get('h_tid='.$tid);
	if(empty($rnt)===false){
		$rnt['h_finished_date']=local_date('Y-m-d H:i',$rnt['h_finished_date']);
		$rnt['next_id']=empty($haul_info['h_tid'])?0:$haul_info['h_tid'];
		return true;
	}
	return false;
}


function JSONResultStr($status,$list,$pars) {
	$reval=array("status"=>$status,
	"result"=>$list);
	$json = json_encode($reval);
	return ($json);
	//return urldecode($json);
}

